<template>
	<view class="container">
		<uni-forms err-show-type="undertext" :value="formData" ref="form" border="true" label-width="100">
			<uni-group title="服务器信息" top="0">
				<uni-forms-item required label="服务器编号" name="serverId">
					<uni-easyinput type="number" trim="all" v-model="formData.serverId" :value="formData.serverId"
						placeholder="请输入服务器编号" />
				</uni-forms-item>
				<uni-forms-item required label="口令" name="password">
					<uni-easyinput type="password" trim="all" v-model="formData.password"
						placeholder="请输入口令" />
				</uni-forms-item>
			</uni-group>
			<ad unit-id="adunit-0f52fd978d21670e"></ad>
			<button style="margin-top: 30rpx;" type="primary" @click="submitForm('form')">提交</button>
		</uni-forms>
		<view>
			<!-- 提示信息弹窗 -->
			<uni-popup ref="message" type="message" :value="popupMessage">
				<uni-popup-message :type="popupMessage.msgType" :message="popupMessage.messageText" :duration="2000">
				</uni-popup-message>
			</uni-popup>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				formData: {
					serverId:'',
					unionName: '',
					titleName:'',
					coordinateX: '',
					coordinateY: '',
					password:'',
					title:0,
					gj:'',
					kxj:'',
					jzs:'',
					titleNames: [{"value": 0,"text": "公爵"	},{"value": 1,"text": "科学家"},{"value": 2,"text": "建筑师"}]
				},
				popupMessage: {
					type: 'center',
					msgType: 'success',
					messageText: '这是一条成功提示',
					value: ''
				},
				rules: {
					serverId: {
						rules: [{
								required: true,
								errorMessage: '请输入服务器编号',
							},
							{
								format: 'int',
								errorMessage: '必须是数字'
							},
						]
					},
					password: {
						rules: [{
								required: true,
								errorMessage: '请输入口令',
							}
						]
					}
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
			this.formData.serverId=getApp().globalData.user.serverId
			this.formData.serverCode=getApp().globalData.user.serverCode
			this.formData.password=getApp().globalData.user.password
		},
		methods: {
			submitForm: function(e) {
				let that = this
				uni.showLoading()
				this.$refs.form.submit().then(addUser => {
					addUser.userId = getApp().globalData.user.id
					addUser.headImg = getApp().globalData.user.headImg
					addUser.nickName = getApp().globalData.user.nickName
					uni.request({
						url: getApp().globalData.url + '/api/title/checkingTitle',
						method: "POST",
						header: {
							'content-type': 'application/json',
							'token':getApp().globalData.token
						},
						data: addUser,
						success: (res) => {
							if (res.data.code == "00000") {
								that.popupMessage.msgType = "success"
								that.popupMessage.messageText = "成功"
								that.$refs.message.open()
								getApp().globalData.user.serverId=that.formData.serverId
								getApp().globalData.user.password=that.formData.password
								uni.redirectTo({
									url: "/pages/applyTitle/applyTitle"
								})
							} else {
								that.popupMessage.msgType = "error"
								that.popupMessage.messageText = res.data.msg
								that.$refs.message.open()
							}
						}
					});
					uni.hideLoading()
					console.log('表单数据信息：', addUser);
				}).catch(err => {
					uni.hideLoading()
					console.log('表单错误信息：', err);
				})
			},
			formReset: function(e) {
				console.log('清空数据')
			},
			goHelp() {
				uni.navigateTo({
					url: "/pages/help/help"
				})
			},
			onload() {
				console.log(1111111111)
				// console.log(param)
				// this.formData.serverId=param.serverId
				// this.formData.serverCode=param.serverCode
				// this.formData.password=param.password
				uni.request({
					url: getApp().globalData.url + '/api/title/getApplyingTitle',
					header: {
						'content-type': 'application/json',
						'token': getApp().globalData.token
					},
					success: (res) => {
						if (res.data.code == "00000") {
							this.unionCount = res.data.data
						} else {
							this.popupMessage.msgType = "error"
							this.popupMessage.messageText = res.data.msg
							this.$refs.message.open()
						}
					}
				});
			}
		},
		onShareAppMessage: function( options ){
		　　var that = this;
		　　var shareObj = {
		　　　　path: '/pages/menu/menu'
		　　};
		　　return shareObj;
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 20rpx 0;
	}

	.container {
		padding: 20rpx;
	}
</style>
